package com.xinshujia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinshujia.dao.DeviceDao;
import com.xinshujia.dao.ResourceDao;
import com.xinshujia.dao.RoleDao;
import com.xinshujia.dto.RoleDTO;
import com.xinshujia.entity.RoleEntity;
import com.xinshujia.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * *************************************
 * *   @author: 野狼上校               **
 * *   @since: 2024-07-12 09:18       **
 * *   @version: 1.0                  **
 * *************************************
 * <p>
 *     角色实现类
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleDao, RoleEntity> implements RoleService {
    private final RoleDao roleDao;
    private final ResourceDao resourceDao;
    private final DeviceDao deviceDao;

    public RoleServiceImpl(RoleDao roleDao, ResourceDao resourceDao, DeviceDao deviceDao) {
        this.roleDao = roleDao;
        this.resourceDao = resourceDao;
        this.deviceDao = deviceDao;
    }

    /**
     * 分页查询
     */
    @Transactional
    @Override
    public Page<RoleDTO> getRoleByPage(Long pageNum, Long pageSize, String roleName) {
        QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasLength(roleName)) {
            queryWrapper.like("role_name", roleName)
                    .or()
                    .like("role_name_zh", roleName);
        }
        Page<RoleEntity> page = new Page<>(pageNum, pageSize);
        Page<RoleEntity> roleEntityPage = roleDao.selectPage(page, queryWrapper);
        Page<RoleDTO> roleDTOPage = new Page<>();
        BeanUtils.copyProperties(roleEntityPage, roleDTOPage);
        List<RoleDTO> roleDTOS = new ArrayList<>();

        // 添加角色拥有的资源
        for(RoleEntity roleEntity : roleEntityPage.getRecords()){
            RoleDTO roleDTO = new RoleDTO();
            BeanUtils.copyProperties(roleEntity, roleDTO);
            // 查询角色拥有的资源，并赋值
            roleDTO.setResources(resourceDao.getResourcesByRoleId(roleDTO.getId()));
            // 添加角色拥有的设备,并赋值
            roleDTO.setDevices(deviceDao.getDevicesByRoleId(roleDTO.getId()));
            roleDTOS.add(roleDTO);
        }
        roleDTOPage.setRecords(roleDTOS);
        return roleDTOPage;
    }

    @Override
    public List<RoleEntity> getRoles() {
        return roleDao.selectList(new QueryWrapper<>());
    }

    /**
     * 给角色分配资源
     */
    @Override
    public boolean setResourcesForRole(Long roleId, String resourceIds) {
        // 删除关联表中的数据
        roleDao.deleteRelationRoleAndResource(roleId);
        if(!resourceIds.isEmpty()){
            // 新增关联表中的数据
            List<Long> resourceIdList = new ArrayList<>();
            String[] split = resourceIds.split(",");
            for (String s : split) {
                resourceIdList.add(Long.parseLong(s));
            }
            return roleDao.setResourcesForRole(roleId, resourceIdList) > 0;
        }
        return true;
    }

    /**
     * 给角色添加设备
     */
    @Override
    public boolean setDevicesForRole(Long roleId, String deviceIds) {
        // 删除关联表中的数据
        roleDao.deleteRelationRoleAndDevice(roleId);
        if(!deviceIds.isEmpty()){
            // 新增关联表中的数据
            List<Long> deviceIdList = new ArrayList<>();
            String[] split = deviceIds.split(",");
            for (String s : split) {
                deviceIdList.add(Long.parseLong(s));
            }
            return roleDao.setDevicesForRole(roleId, deviceIdList) > 0;
        }
        return true;
    }


}
